const http = require("http");
const fs = require("fs");
const path = require("path")

http.createServer((req,res)=>{
    var urlStr =req.url;
    var filePath = path.join(__dirname,"/test.html");
    var imgPath = path.join(__dirname,"/icon.png");
    //当请求资源路径是'/'的时候，响应网页文件到浏览器 
    if(urlStr=="/"){
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
        res.write(fileContent);
        res.end();
    }
    else if(urlStr=="/icon.png"){
        var imgContent = fs.readFileSync(imgPath);
        res.writeHead(200,{"Content-Type":"image/png"});
        res.write(imgContent);
        res.end();

    }
}).listen(8082);

console.log("server is listening 8082");